<%--
    File        : Mediums
    Created     : 10.6.2014
    Author      : Václav Bukovský
    Description : Jsp page which serve all operation with mediums and displays all mediums in database with their
    label, type and movies.
    Operation should be : create medium, delete medium, search medium by label and update medium.
--%>
<%@page contentType="text/html" pageEncoding="UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <head>
        <title>Mediums Library</title>
        <link rel="stylesheet" type="text/css" href="library.css">
    </head>
    <body>
    <div style="text-align: center">
        <div>
            <h1>Mediums Library</h1>
            <form method="post" action="<c:url value="index"/>" name="back" style="margin-bottom: 0;">
                <input type="submit" value="Home">
            </form>
        </div>
    </div>
    <br/>

    <!-- Create medium. -->
    <div class="left">
        <div>
            <h3>Create new medium</h3>
            <form action="<c:url value="/createMedium"/>" name="newCategory" method="post">
                <table border="1">
                    <tr>
                        <th>Medium label:</th>
                        <td><input type="text" name="label" required="1" maxlength="50"/></td>
                    </tr>
                    <tr>
                        <th>Medium Type:</th>
                        <td>
                            <select name="typeDropdown" required="1">
                                <option value="DVD" name="DVDType">DVD</option>
                                <option value="CD" name="CDType">CD</option>
                                <option value="Blu-Ray" name="BluType">Blu-Ray</option>
                                <option value="Video-tape" name="Video-tape">Video-tape</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th>Insert medium into category:</th>
                        <td>
                            <select name="categoriesDropdown" required="1">
                                <c:forEach items="${categories}" var="category">
                                    <option value="<c:out value='${category.name}'/>">${category.name}</option>
                                </c:forEach>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th>Enter films here:</th>
                        <td>
                            <textarea name="movies" rows="5" cols="45" required placeholder="Write films here and separate them with ','" ></textarea>
                        </td>
                    </tr>
                </table>
                <input type="Submit" value="Create" />
            </form>
            <c:if test="${not empty createError}">
                <h4 style="color: red; background-color: black"><c:out value="${createError}"/></h4>
            </c:if>
            <c:if test="${not empty createMessage}">
                <h4 style="color: black; background-color: green;"><c:out value="${createMessage}"/></h4>
            </c:if>
        </div>
        <div>
            <h3>All Mediums</h3>
            <c:if test="${not empty listMediums}">
                <c:set var="msg" value="${listMediums}" scope="page" />
                <c:out value="${msg}" escapeXml="false" default="Nothing to show!"/>
            </c:if>
        </div>
    </div>
    <div class="right">

        <!-- Search medium -->
        <div>
            <form action="<c:url value="search"/>" name="searchMedium" method="post">
                <table border="1">
                    <tr>
                        <th>Search for medium</th>
                        <td><input type="text" name="search" required/></td>
                    </tr>
                </table>
                <input type="submit" value="Search">
            </form>
            <c:if test="${not empty searchError}">
                <h4 style="color: red; background-color: black"><c:out value="${searchError}"/></h4>
            </c:if>
        </div>
        <div>
            <c:if test="${not empty resultSearch}">
                <c:set var="msg" value="${resultSearch}" scope="page" />
                <c:out value="${msg}" escapeXml="false" default="Nothing to show!"/>
            </c:if>
        </div>

        <hr/>

        <!-- Delete medium -->
        <div>
            <form action="<c:url value="/deleteMedium"/>" name="deleteMedium" method="post">
                <table border="1">
                    <tr>
                        <th>Enter medium label to delete: </th>
                        <td><input type="text" name="delete" required/></td>
                    </tr>
                </table>
                <input type="submit" value="Delete">
            </form>

            <!-- Delete error -->
            <c:if test="${not empty deleteError}">
                <h4 style="color: red; background-color: black"><c:out value="${deleteError}"/></h4>
            </c:if>
            <c:if test="${not empty deletedMed}" >
                <h4 style="color: black; background-color: green"> Deletion successful. Medium "${deletedMed}" removed.</h4>
            </c:if>
        </div>

        <hr/>

        <!-- Update medium -> search pane -->
        <div>
            <form action="<c:url value="/updateMedium"/>" name="updateMedium" method="post">
                <table border="1">
                    <tr>
                        <th>Enter medium label to update: </th>
                        <c:choose>
                            <c:when test="${not null and not empty searchedMedium}">
                                <td><input type="text" name="update" value="${searchedMedium}" required/></td>
                            </c:when>
                            <c:otherwise>
                                <td><input type="text" name="update" required/></td>
                            </c:otherwise>
                        </c:choose>
                    </tr>
                </table>
                <c:choose>
                    <c:when test="${not null and not empty searchedMedium}">
                        <input type="submit" value="Update" hidden />
                    </c:when>
                    <c:otherwise>
                        <input type="submit" value="Update" />
                    </c:otherwise>
                </c:choose>
            </form>

            <!-- Update error -->
            <c:if test="${not empty updateError}">
                <h4 style="color: red; background-color: black"><c:out value="${updateError}"/></h4>
            </c:if>
        </div>

        <br/>

        <!-- Update medium -> update value -->
        <div>
            <c:if test="${not null and not empty searchedMedium}">
                <form action="<c:url value="/updateMedium"/>" name="updateMedium" method="post">
                    <table border="1">
                        <tr>
                            <th>Medium label:</th>
                            <td><input type="text" name="uLabel" value="${uLabel}" required="1" maxlength="50"/></td>
                        </tr>
                        <tr>
                            <th>Medium Type:</th>
                            <td>
                                <select name="uTypeDropdown" required="1">
                                    <option value="DVD" name="DVDType">DVD</option>
                                    <option value="CD" name="CDType">CD</option>
                                    <option value="Blu-Ray" name="BluType">Blu-Ray</option>
                                    <option value="Video-tape" name="Video-tape">Video-tape</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <th>Insert medium into category:</th>
                            <td>
                                <select name="uCategoriesDropdown" required="1">
                                    <c:forEach items="${categories}" var="category">
                                        <c:choose>
                                            <c:when test="${category.name eq ucategory}">
                                                <option value="<c:out value='${category.name}'/>" selected>${category.name}</option>
                                            </c:when>
                                            <c:otherwise>
                                                <option value="<c:out value='${category.name}'/>">${category.name}</option>
                                            </c:otherwise>
                                        </c:choose>
                                    </c:forEach>
                                </select>
                            </td>
                        </tr>
                    </table>
                    <input type="Submit" value="Submit" />
                    <input type="Submit" value="Cancel" name="Cancel" />
                </form>
            </c:if>

            <!-- Dve media se stejnym labelem -->
            <c:if test="${not empty uError}">
                <h4 style="color: red; background-color: black"><c:out value="${uError}"/></h4>
            </c:if>
        </div>    
    </div>

    </body>
</html>